/***************************************************************************
 *   Copyright (C) 2007 by Luca Bellonda                                   *
 *   lbell @ tsc4.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

package lb.prove;

import java.io.IOException;
import java.net.URL;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;

public class RssRead extends DefaultHandler {

	boolean newData = false;
	String sourceUrl ;
	Context context; 
	DBHelper db;
	public RssRead(){;}
	
	RssItem currItem = null;
	StringBuilder inputText = new StringBuilder(); 
	public void startElement (String uri, String name,
			String qName, Attributes atts)
	{
		String theName = qName.trim();
		if (theName.equals("item")){
			currItem = new RssItem();
		}
	}
	
	public void endElement (String uri, String name, String qName)
	{
		String elmtName = qName.trim();
		if (elmtName.equals("item")){
			addItem(currItem);
			currItem = null;
		} else if (elmtName.equals("title")){
			if(null!=currItem)
				currItem.title = inputText.toString().trim();
		} else if (elmtName.equals("description")){
			if(null!=currItem)
				currItem.body = inputText.toString().trim();
		} else if (elmtName.equals("guid")){
			if(null!=currItem)
				currItem.guid = inputText.toString().trim();
		} else if (elmtName.equals("pubDate")){
			if(null!=currItem)
				currItem.setPubDate(inputText.toString().trim());
		} else if (elmtName.equals("link")){
			if(null!=currItem)
				currItem.url = inputText.toString().trim();
		}
		inputText.setLength(0);
	}
	
	public void characters (char ch[], int start, int length)
	{
		this.inputText.append(ch, start, length);
	}
	public boolean getRSSEntries(String theUrl, Context contextParam, DBHelper dbParam )
	{
		context = contextParam ;
		db = dbParam ;
		sourceUrl = theUrl ;
		try
		{
			// Now parse the data
			try{
				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp = spf.newSAXParser();
				XMLReader xr = sp.getXMLReader();
				xr.setContentHandler(this);
				URL url = new URL(sourceUrl);
				xr.parse(new InputSource(url.openStream()));
			}
			catch (IOException e){
				e.printStackTrace();
			}
			catch (SAXException e){
				e.printStackTrace();
			}
			catch (ParserConfigurationException e){
				e.printStackTrace();
			}
		}catch ( Exception e)
		{
			e.printStackTrace();
		}
		return newData ;
	} // getRSSEntries()
	void addItem(RssItem item)
	{
		if(db.addEntry(item.guid, item.title, item.body, "", new Date(), item.url ))
			newData = true ;
	}
}

